#include <Arduino.h>
#include <ESP8266WiFi.h>


// WiFi settings
const char *ssid="LWT";
const char *password="Wjs717601";
const char* host = "192.168.4.1";


void setup() 
{
  Serial.begin(115200);
  delay(10);
  pinMode(D6, INPUT);
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) 
  {
    delay(500);
    Serial.print(".");
  }
  Serial.println("WiFi connected");
}


void loop() 
{
  delay(5000);
  int val = digitalRead(D6);
  WiFiClient client;
  const int httpPort = 80;

  if (!client.connect(host, httpPort)) 
  {
    Serial.println("client failed");
    return;
  }
  String url = "/led/";
  if(val) {url+="1";}
  else  {url+="0";}

  Serial.print("Requesting URL: ");
  Serial.println(url);

  client.print(String("GET ") + url + " HTTP/1.1\r\n" +
                              "Host: " + host + "\r\n" + 
                              "Connection: close\r\n\r\n");
  unsigned long timeout = millis();

  while (client.available() == 0) 
  {
    if (millis() - timeout > 5000) 
    {
      Serial.println(">>> Client Timeout !");
      client.stop();
      return;
    }
  }

  while(client.available())//以\r为结尾
  {
    String line = client.readStringUntil('\r');
    Serial.print(line);
  }

  Serial.println();
  Serial.println("closing connection");



}